package cn.edu.ldh.toolapp.stores;

import cn.edu.ldh.toolapp.entity.NoteItem;
import cn.edu.ldh.toolapp.entity.NoteItemRes;
import cn.edu.ldh.toolapp.util.ResUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmDatabase;
import ohos.data.orm.OrmPredicates;
import ohos.data.orm.annotation.Database;
import ohos.data.rdb.RdbOpenCallback;
import ohos.data.rdb.RdbStore;
import ohos.data.rdb.ValuesBucket;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import java.util.List;

@Database(entities = {NoteItem.class}, version = 1)
public class NoteStore extends OrmDatabase {

    private static final String DATABASE_LINT = "stuMemoStore";
    private static final String DATABASE_NAME = "stuMemoStore.db";
    private static final int SUCCESS_CODE = 200;
    private static final int FAIL_CODE = 500;
    private static NoteStore instance;
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 1, "NoteStore");
    static OrmContext ormContext;

    //单例模式
    public static NoteStore getInstance() {
        if(instance == null) {
            instance = new NoteStore();
        }
        return instance;
    }

    //创建数据库
    public void createDataBase(AbilitySlice slice) {
        HiLog.error(LABEL, "开始创建");
        DatabaseHelper helper = new DatabaseHelper(slice);
        ormContext = helper.getOrmContext(DATABASE_LINT, DATABASE_NAME, NoteStore.class);
        HiLog.error(LABEL, "数据库创建成功");
    }

    /**
     * 获取所有
     * @return NoteItemList;
     */
    public List<NoteItem> getAllNote() {
        if(ormContext == null) {
            HiLog.error(LABEL, "获取失败");
            return null;
        } else {
            OrmPredicates list = ormContext.where(NoteItem.class);
            return ormContext.query(list);
        }
    }

    /**
    * 插入日记
    * @param noteItem;
    * @return res;
    * */
    public NoteItemRes insertNote(NoteItem noteItem) {
        if(ormContext == null) {
            return ResUtil.backNoteItemRes(FAIL_CODE, "操作失败", null);
        } else {
            boolean insertRes = ormContext.insert(noteItem);
            boolean flushRes = ormContext.flush();
            if (insertRes || flushRes) {
                return ResUtil.backNoteItemRes(SUCCESS_CODE, "添加成功", noteItem);
            } else {
                return ResUtil.backNoteItemRes(FAIL_CODE, "添加失败", null);
            }
        }
    }

    /**
     * 更新日记
     * @param noteItem;
     * @return res
     * */
    public NoteItemRes updateNote(NoteItem noteItem) {
        ValuesBucket valuesBucket = new ValuesBucket();
        OrmPredicates oldNote = ormContext.where(NoteItem.class).equalTo("noteId", noteItem.getNoteId());
        if(oldNote == null) {
            return ResUtil.backNoteItemRes(FAIL_CODE, "无数据", null);
        } else {
            valuesBucket.putString("noteTitle", noteItem.getNoteTitle());
            valuesBucket.putString("noteContent", noteItem.getNoteContent());
            ormContext.update(oldNote, valuesBucket);
            return ResUtil.backNoteItemRes(SUCCESS_CODE, "更新成功", null);
        }
    }

    /**
     * 删除日记
     * @param noteId;
     * @return res;
     * */
    public NoteItemRes deleteNote(Integer noteId) {
        OrmPredicates queryNote = ormContext.where(NoteItem.class).equalTo("NoteId", noteId);
        List<NoteItem> noteList = ormContext.query(queryNote);
        boolean deleteRes = ormContext.delete(noteList.get(0));
        boolean flushRes = ormContext.flush();
        if(deleteRes || flushRes) {
            return ResUtil.backNoteItemRes(SUCCESS_CODE, "删除成功", null);
        } else {
            return ResUtil.backNoteItemRes(FAIL_CODE, "删除失败", null);
        }
    }

    @Override
    public int getVersion() {
        return 1;
    }

    @Override
    public RdbOpenCallback getHelper() {
        return new RdbOpenCallback() {
            @Override
            public void onCreate(RdbStore rdbStore) {
                HiLog.error(new HiLogLabel(HiLog.LOG_APP, 0xD00201, "Data_Log"), "数据库创建成功");
            }

            @Override
            public void onUpgrade(RdbStore rdbStore, int i, int i1) {

            }
        };
    }
}
